package com.xiaxiang.xiawan.share;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.JSON.MyJsonUtil;
import com.dg.acb.utils.configdata.DGConfigData;
import com.xiaxiang.xiawan.R;
import com.xiaxiang.xiawan.video.VideoInfoListener;
import com.xiaxiang.xiaxwan.ar.data.XXARMaterialVideoData;
import com.xx.xiawan.videoplayer.Utils;
import com.xx.xiawan.videoplayer.XXVideoPlayer;
import com.xx.xiawan.videoplayer.XXVideoView;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2017-10-28.
 */

public class SharePreviewVidewActivity extends Activity implements View.OnClickListener{
    private XXVideoPlayer mVideoView;
    private boolean needResume;
    private String mVideoPath = null;
    private int mRotation = 0;
    private int mVideoRotation;
    private DGConfigData mConfigData;
    private XXARMaterialVideoData mMaterialData;
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.video_share_activity);
        mConfigData = new DGConfigData(this);
        XXVideoView videoView = (XXVideoView) findViewById(R.id.videoView);
        ImageView imgPlayer = (ImageView) this.findViewById(R.id.img_start);
        imgPlayer.setOnClickListener(this);
        ImageView imgBack = (ImageView)this.findViewById(R.id.imgBack);
        imgBack.setOnClickListener(this);
        Button btnShare = (Button)this.findViewById(R.id.btnShare);
        btnShare.setOnClickListener(this);
        Intent intent = getIntent();
        if (intent != null)
        {
            mMaterialData = intent.getParcelableExtra(Utils.MATERIAL_VIDEO_DATA_KEY);
            if(mMaterialData != null){
                mVideoPath = mMaterialData.mVideoPath;
            }
        }
        mVideoView = new XXVideoPlayer(this);
        mVideoView.setVideoView(videoView);
        mVideoView.setOnInfoListener(mOnInfoListener);
        mVideoView.setVideoPath(mVideoPath);
    }

    private void startVideo(){
        if(mVideoView != null){
            mVideoView.start();
        }
    }

    private void pauseVideo(){
        if(mVideoView != null){
            mVideoView.pause();
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        pauseVideo();
    }

    @Override
    protected void onResume() {
        super.onResume();
        startVideo();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mVideoView != null){
            mVideoView.stopPlayback();
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.img_start){
            startVideo();
        } else if (id == R.id.btnShare) {
            Intent intent = new Intent("com.xiaxiang.xiawan.share_preview");
            intent.setPackage(getPackageName());
            if(mMaterialData != null){
               String config =  mConfigData.getConf(mVideoPath);
                Log.i("up","mVideoPath:" + mVideoPath);
                Log.i("up","config:" + config);
                if(config != null && config.length() >0){
                    try {
                        JSONObject object = new JSONObject(config);
                        mMaterialData.mMaterialId = object.getString("materialId");
                        mMaterialData.mMaterialUrl = object.getString("materialUrl");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                mMaterialData.mVideoPath = mVideoPath;
                Log.i("up","1:" + mMaterialData.mMaterialId + mMaterialData.mMaterialUrl);
                intent.putExtra(Utils.MATERIAL_VIDEO_DATA_KEY,mMaterialData);
            }
            startActivity(intent);
            finish();
        } else if (id == R.id.imgBack) {
            finish();
        }
    }

    private VideoInfoListener mOnInfoListener = new VideoInfoListener(){

        @Override
        public void onPrepared() {

        }

        @Override
        public void onCompletion() {

        }

        @Override
        public void mediaInfoBufferingStart(int extra) {
            System.out.println("正在缓冲----\"");
            //开始缓存，暂停播放
//                    if (isPlaying()) {
//                        stopPlayer();
            if (mVideoView != null) {
                mVideoView.pause();
            }
            needResume = true;
        }

        @Override
        public void mediaInfoBufferingEnd(int extra) {
            System.out.println("缓冲完成----");
            //缓存完成，继续播放
            if (needResume)
//                        startPlayer();
                if (mVideoView != null) {
                    mVideoView.start();
                }
        }

        @Override
        public void mediaInfoRenderingStart(int extra) {
            System.out.println("缓冲完成----");
            //缓存完成，继续播放
            if (needResume)
//                        startPlayer();
                if (mVideoView != null) {
                    mVideoView.start();
                }
        }

        @Override
        public void mediaInfoBufferingBytesUpdate(int extra) {

        }

        @Override
        public void doOther(int extra) {
            mVideoRotation=extra;
        }
    };
}
